<?php

class Core_Component_Cms_Model_Category extends Core_Model_Entity_Dynamic_Version {

    protected $tableName = '{t}cms_category';
    
    public function getChildCategories(){
        return App::getModel('cms/category')
                ->getQuery()
                ->condition('m.id_path', $this->getIdPath(), 'starts');
    }
    
    public function getContents(){
        $ids = $this->getChildCategories()->fetch()->getFieldValues('id');
        return App::getModel('cms/content')
                ->getQuery()
                ->condition('m.category_id', $ids, 'in')
                ->condition('v.website_id', App::getRunningWebsite()->getId());
    }
    
    protected function beforeInsert(){
        $this->setWebsiteId(App::getRunningWebsite()->getId());
        if(!$this->issetData('parent_id')){
            $this->setParentId('0');
        }
        parent::beforeInsert();
    }
    
    protected function afterInsert() {
    	$this->markNoChanged();
    	$idPath = $this->getId().'_';
    	if($this->issetData('parent_id') && $this->getParentId() != 0){
    		$parent = new self();
    		$idPath = $parent->load($this->getParentId())->getIdPath().$idPath;
    	}
    	$sql = 'UPDATE `'.$this->getTableName().'` SET'
    		 . ' `id_path`=:idPath:s'
    		 . ' WHERE `id` =:id:i';
    	$this->getDb()->setSql($sql, array('id'=>$this->getId(), 'idPath'=>$idPath))->execute();
    }
    
    protected function beforeUpdate(){
        if(!$this->issetData('parent_id')){
            $this->setParentId('0');
        }
        parent::beforeInsert();
    }    
    
    protected function afterUpdate(){
    	if($this->propertyIsChanged('parent_id')){
    		$parent = new self();
            $oldIdPath = $this->getIdPath();
    		if($this->issetData('parent_id') && $this->getParentId() != 0){
    			$parent->load($this->issetData('parent_id'));
    			$idPath = $parent->loadBy(array('id'=>$this->getParentId(),'website_id'=>App::getRunningWebsite()->getId()))
                                 ->getIdPath().$this->getId().'_';
    		}else{
    			$idPath = $this->getId().'_';
    		}
	    	$sql = 'UPDATE `'.$this->getTableName().'` SET'
	    		 . ' `id_path`=concat(:newIdPath:s, substring(`id_path`,'.(strlen($oldIdPath) + 1).'))'
	    		 . ' WHERE `id_path` LIKE :oldIdPath:s';
	    	$this->getDb()->setSql($sql, array('newIdPath'=>$idPath, 'oldIdPath'=>$this->getIdPath().'%'))->execute();
    	}
    }
    
    protected function deleteSelf() {
        $childIds = $this->getChildCategories()->fetch()->getFieldValues('id');
        Soul_Db_Factory::createDelete()
               ->delete($this->getTableName())
               ->condition('id', $childIds, 'in')
               ->execute();
        $this->setData('child_ids', $childIds);
    }
    
    protected function deleteVersion() {
        $delete = Soul_Db_Factory::createDelete();
        $delete->delete($this->getVersionTableName())
               ->condition('main_id', $this->getChildIds(), 'in')
               ->execute();
    }
    
    protected function afterDelete(){
        Soul_Db_Factory::createUpdate()
               ->update('{t}cms_content')
               ->set('category_id', 0)
               ->condition('category_id', $this->getChildIds(), 'in')
               ->execute();
    }
}